#include "StdAfx.h"
#include "GraphicShowVideo.h"
#include "MultiMediaDlg.h"

IMPLEMENT_DYNAMIC(GraphicShowVideo,CWnd)

GraphicShowVideo *g_Instance;

GraphicShowVideo* GraphicShowVideo::ShareInstance()
{
	return g_Instance;
}

GraphicShowVideo::GraphicShowVideo(void)
{
	g_Instance = this; 
	m_Sub = NULL;
}

GraphicShowVideo::~GraphicShowVideo(void)
{
}
BEGIN_MESSAGE_MAP(GraphicShowVideo, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_WINDOWPOSCHANGED()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

int GraphicShowVideo::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_Sub = new TestSubTitle2(this);
	m_Sub->Create(TestSubTitle2::IDD, this);
	isShowSubtitle = false;
	m_bCheckShow = false;
	CRect rc; 
    CWnd *pWnd =this->GetParentOwner();
	
	pWnd->GetWindowRect(rc);
	m_Sub->SetWindowPos(&CWnd::wndTop, rc.left, rc.top + rc.Height() * 0.75, rc.Width(), rc.Height()/5, SWP_NOOWNERZORDER);
    m_Sub->ShowWindow(SW_HIDE);

	m_showSmall.Create(NULL, NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, NULL);
	m_showSmall.ShowWindow(SW_HIDE);
	m_showSmall.SetWindowPos(&CWnd::wndTop,0,0,  rc.Width()/6,rc.Height()/6, SWP_NOOWNERZORDER);

	checkSubTitle();
	return 0;
}

void GraphicShowVideo::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	CRect rc; 
	CWnd *pWnd =this->GetParentOwner();
	this->GetWindowRect(&rc);
	m_Sub->SetWindowPos(&CWnd::wndTop, rc.left, rc.top + rc.Height() * 0.75, rc.Width(), rc.Height()/5, SWP_NOOWNERZORDER);
	if(CMultiMediaDlg::ShareInstance()->m_bIsPlaying == false )
	{   
		
		CDC *pDC = this->GetDC();
		if(CMultiMediaDlg::ShareInstance()->m_Audio)
		{
		   GraphicShowVideo::OnEraseBkgnd(pDC);
		}
		else

		{
		HDC _hDC = pDC->GetSafeHdc();
		CMultiMediaDlg::ShareInstance()->m_VideoMng.RePaintVideo(_hDC);
		}
	}
}


void  GraphicShowVideo::SetWndParentForVideoPlayerManager()
{
	CMultiMediaDlg::ShareInstance()->m_VideoMng.SetWindowParent(this);
}


BOOL GraphicShowVideo::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message== WM_GRAPHNOTIFY)
	{
		HandleEventFromGraph();
	}

	return CWnd::PreTranslateMessage(pMsg);
}
void GraphicShowVideo::HandleEventFromGraph()
{
	//MediaPlay();
	HRESULT hr = S_OK;
	long evCode, param1,param2, evCode2, param3, param4;
	if(!CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaEventEx)
	{
		AfxMessageBox(_T("MediaEventEx interface error"));
	}
	
	if(m_bCheckShow)
	{
	    m_showSmall.ShowWindow(SW_SHOW);
		m_bCheckShow = false;
	}

	while (SUCCEEDED(CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaEventEx->GetEvent(&evCode, &param1, &param2, 0)))
	{
		switch(evCode)
		{
		case EC_OLE_EVENT:
			{
				
				m_Sub->SetTextCaption((BSTR)param2, false);
			}
			break;
		case EC_COMPLETE:
			//SetPlayListPropertiesValueWhenComplete();	
			
			break;
		}
		hr = CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaEventEx->FreeEventParams(evCode, param1, param2);
	}
}

void GraphicShowVideo::checkSubTitle()
{
 
	if (isShowSubtitle)
	{
		m_Sub->ShowWindow(SW_SHOW);
	}
	else
	{
		m_Sub->ShowWindow(SW_HIDE);
	}

}


BOOL GraphicShowVideo::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	CRect rect;

	GetClientRect(&rect);
	CBrush myBrush(RGB(0, 0, 0));    // dialog background color
	CBrush *pOld = pDC->SelectObject(&myBrush);
	BOOL bRes  = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
	pDC->SelectObject(pOld);    // restore old brush
	return bRes;
	return CWnd::OnEraseBkgnd(pDC);
}

